import 'dart:math';
import 'dart:typed_data';
import 'dart:ui';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

///
/// [author]: liMing
/// [Date]: 2021-08-02  12:57
///
class PageDraw extends CustomPainter {
  final double step = 10; // 小格边长
  final List<Offset>points=[
    Offset(-120, -20),
    Offset(-80, -80),
    Offset(-40, -40),
    Offset(0, 0),
    Offset(0, -100),
    Offset(40, -140),
    Offset(80, -160),
    Offset(120, -100),

  ];
  @override
  void paint(Canvas canvas, Size size) {
    canvas.translate(size.width / 2, size.height / 2);
    var paint = Paint()..color = Colors.orangeAccent;
    // canvas.drawCircle(Offset(0, 0), 30, paint);
    // canvas.drawCircle(Offset(0, 0), 5, paint..color=Colors.blue);
    _drawDot(canvas,size);
    // _drawPoint(canvas);
    // _drawPointLine(canvas);
    // _drawPointPolygon(canvas);
    // _drawRawPoints(canvas);
    drawLine(canvas,size);
    // drawRect(canvas);
    // drawRRect(canvas);
    // drawDRRect(canvas);
    // drawFill(canvas);
    drawColor(canvas);
  }

  ///画网格
  _drawGrid(Canvas canvas, Size size) {
    var paint = Paint()
      ..color = Colors.black
      ..strokeWidth = .5;
    canvas.save();
    for (int i = 0; i < size.height / 2 / step; i++) {
      canvas.drawLine(Offset(0, 0), Offset(size.width / 2, 0), paint);
      canvas.translate(0, step);
    }
    canvas.restore();
    canvas.save();

    for (int i = 0; i < size.width / 2 / step; i++) {
      canvas.drawLine(Offset(0, 0), Offset(0, size.height / 2), paint);
      canvas.translate(step, 0);
    }
    canvas.restore();
  }

  ///网格反转
  _drawDot(Canvas canvas,Size size){
    canvas.save();
    _drawGrid(canvas, size);
    canvas.restore();

    canvas.save();
    canvas.scale(1,-1);
    _drawGrid(canvas, size);
    canvas.restore();

    canvas.save();
    canvas.scale(-1,1);
    _drawGrid(canvas, size);
    canvas.restore();

    canvas.save();
    canvas.scale(-1,-1);
    _drawGrid(canvas, size);
    canvas.restore();


  }


  ///画点
  _drawPoint(Canvas canvas){
    var paint = Paint()..color = Colors.red..strokeWidth=8..strokeCap=StrokeCap.round;
    canvas.drawPoints(PointMode.points, points, paint);
  }
  ///亮点之间连线，如果点是奇数个，那么最后一个点将没有用
  _drawPointLine(Canvas canvas){
    var paint = Paint()..color = Colors.red..strokeWidth=3..strokeCap=StrokeCap.round;
    canvas.drawPoints(PointMode.lines, points, paint);
  }

  ///所有点连线
  _drawPointPolygon(Canvas canvas){
    var paint = Paint()..color = Colors.red..strokeWidth=3..strokeCap=StrokeCap.round;
    canvas.drawPoints(PointMode.polygon, points, paint);
  }

  ///画点
  void _drawRawPoints(Canvas canvas) {
    Float32List pos = Float32List.fromList([
      -120, -20,-80, -80,-40,
      -40,0, -100,40, -140,
      80, -160,120, -100]);
    var paint=Paint()
      ..color = Colors.red
      ..style = PaintingStyle.stroke
      ..strokeWidth = 10
      ..strokeCap = StrokeCap.round;
    canvas.drawRawPoints(PointMode.points, pos, paint);
  }

  ///画坐标轴
  drawLine(Canvas canvas,Size size){
    var paint=Paint()
      ..color = Colors.blue
      ..style = PaintingStyle.stroke
      ..strokeWidth = 1.5
      ..strokeCap = StrokeCap.round;
    canvas.drawLine(Offset(-size.width,0), Offset(size.width,0), paint);
    canvas.drawLine(Offset(size.width/2,0), Offset(size.width/2-10,-10), paint);
    canvas.drawLine(Offset(size.width/2,0), Offset(size.width/2-10,10), paint);


    canvas.drawLine(Offset(0,size.height), Offset(0,-size.height), paint);
    canvas.drawLine(Offset(0,size.height/2), Offset(-10,size.height/2-10), paint);
    canvas.drawLine(Offset(0,size.height/2), Offset(10,size.height/2-10), paint);


  }

  ///绘制矩形的五种构造
  drawRect(Canvas canvas){
   var _paint=Paint()..color=Colors.blue..strokeWidth=1.5;
   //矩形中心构造
   Rect rectFromCenter = Rect.fromCenter(center: Offset(0,0), width: 160, height: 160);
   canvas.drawRect(rectFromCenter, _paint);
   //矩形坐上右下构造(x,y,x,y)
   Rect rectFromLTRB = Rect.fromLTRB(-120,-120,-80,-80);
   canvas.drawRect(rectFromLTRB, _paint..color=Colors.red);
   //矩形左上宽高构造
   Rect rectFromLTWH = Rect.fromLTWH(80,-120,40,40);
   canvas.drawRect(rectFromLTWH, _paint..color=Colors.orange);
   //矩形内切圆构造
   Rect rectFromCircle = Rect.fromCircle(center:Offset(100, 100),radius: 20);
   canvas.drawRect(rectFromCircle, _paint..color=Colors.green);
   //矩形两点构造
   Rect rectFromPoints = Rect.fromPoints(Offset(-80, 80),Offset(-120, 120));
   canvas.drawRect(rectFromPoints, _paint..color=Colors.purple);

  }

  ///绘制圆角矩形
  drawRRect(Canvas canvas){
    var _paint=Paint()..color=Colors.blue..strokeWidth=1.5;
    //矩形中心构造
    Rect rectFromCenter = Rect.fromCenter(center: Offset(0,0), width: 160, height: 160);
    canvas.drawRRect(RRect.fromRectXY(rectFromCenter, 40, 20), _paint);
    //矩形坐上右下构造(x,y,x,y)xY控制椭圆
    canvas.drawRRect(RRect.fromLTRBXY(-120,-120,-80,-80, 40, 20), _paint..color=Colors.red);
    //矩形左上右下构造
    canvas.drawRRect(RRect.fromLTRBR(80,-120,120,-80, Radius.circular(10)), _paint..color=Colors.orange);
    //矩形内切圆构造
    Rect rectFromCircle = Rect.fromCircle(center:Offset(100, 100),radius: 20);
    canvas.drawRRect(RRect.fromLTRBAndCorners(80, 80, 120, 120,bottomLeft:Radius.circular(20),bottomRight: Radius.circular(10)), _paint..color=Colors.green);
    //矩形两点构造
    Rect rectFromPoints = Rect.fromPoints(Offset(-80, 80),Offset(-120, 120));
    canvas.drawRRect(RRect.fromRectAndCorners(rectFromPoints,bottomLeft: Radius.circular(10)), _paint..color=Colors.purple);

  }

  ///绘制两个圆角矩形差域
  drawDRRect(Canvas canvas){
    var _paint=Paint()..color=Colors.blue..strokeWidth=1.5;
    Rect outRect=Rect.fromCenter(center: Offset(0,0), width: 160, height: 160);
    Rect inRect=Rect.fromCenter(center: Offset(0,0), width: 100, height: 100);

    canvas.drawDRRect(RRect.fromRectXY(outRect, 20, 20), RRect.fromRectXY(inRect, 20, 20), _paint);

    Rect outRect2=Rect.fromCenter(center: Offset(0,0), width: 60, height: 60);
    Rect inRect2=Rect.fromCenter(center: Offset(0,0), width: 40, height: 40);

    canvas.drawDRRect(RRect.fromRectXY(outRect2, 15, 15), RRect.fromRectXY(inRect2, 10, 10), _paint..color=Colors.green);


  }
  ///绘制类园
  drawFill(Canvas canvas){
    var _paint=Paint()..color=Colors.red..strokeWidth=1..strokeCap=StrokeCap.square..style=PaintingStyle.stroke;
    //圆
    canvas.save();
    canvas.translate(-120, 0);
    canvas.drawCircle(Offset(0, 0), 30, _paint);
    canvas.restore();
    //椭圆
    Rect rect=Rect.fromCenter(center: Offset(0,0), width: 60, height: 40);
    canvas.drawOval(rect, _paint);
    //圆弧
    canvas.save();
    canvas.translate(120, 0);
    Rect rect1=Rect.fromCenter(center: Offset(0,0), width: 60, height: 60);
    //drawArc(矩形区域,起始弧度,扫描弧度,是否连中心,画笔)
    canvas.drawArc(rect1, 0, pi/2*3, true, _paint);
    canvas.restore();

  }
  ///绘制颜色
  drawColor(Canvas canvas){
    canvas.drawColor(Colors.red, BlendMode.lighten);
  }
  ///画笔着色器
  drawShader(Canvas canvas){
    var colors = [
      Color(0xFFF60C0C),
      Color(0xFFF3B913),
      Color(0xFFE7F716),
      Color(0xFF3DF30B),
      Color(0xFF0DF6EF),
      Color(0xFF0829FB),
      Color(0xFFB709F4),
    ];
    var pos=[1.0/7,2.0/7,3.0/7,4.0/7,5.0/7,6.0/7,1];
    var paint=Paint();
    // paint.shader= LinearGradient();
  }
  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return true;
  }
}
